package auctions;

import jade.core.Agent;

import jade.domain.FIPANames;

import jade.lang.acl.ACLMessage;

import jade.proto.IteratedAchieveREInitiator;

import java.util.Vector;


public class DutchSeller extends IteratedAchieveREInitiator {
    int numOfItems;
    
    int price;

    private static final float decrease = (float)0.9;
    
    int minPrice;
    
    private boolean done;
    
    public DutchSeller(Agent agent, ACLMessage msg) {
        super(agent, msg);
        String content = msg.getContent();
        numOfItems = SellerAgent.getNumOfItemsFromMessage(content);
        price = SellerAgent.getPriceFromMessage(content);
        minPrice = Math.round((float)0.6 * price);
        done = false;
        ((SellerAgent)myAgent).addMessage("Auction started.\nThere are " + 
            numOfItems + " items for sale. The starting price is " + price);
    }
    
    protected void handleAllResultNotifications(Vector notifications,
                                              Vector nextRequests) {
        if(price < minPrice) {
            if(numOfItems > 0) {
                ((SellerAgent)myAgent).addMessage("There are " + numOfItems + 
                    " left unsold.");
            } else {
                ((SellerAgent)myAgent).addMessage("There are no more items for" +
                    " sale. The auction is finished.");
            }
            done = true;
            return;
        }
        if(numOfItems >0) {
            int lastPrice = price;
            //update the price
            price = Math.round(price * decrease);
            //send offers to agents
            for(int i = 0; i < notifications.size();i++) {
                ACLMessage m = (ACLMessage)notifications.get(i);
                if((m.getPerformative() == ACLMessage.INFORM)) {
                    int items = Integer.parseInt(m.getContent());
                    //this agent wants to buy some items
                    if(items > 0) {
                        ((SellerAgent)myAgent).addMessage("Agent " + 
                            m.getSender().getLocalName() + " has bought " + 
                            items + " items at price " + lastPrice);
                        //decrease the number of items
                        numOfItems -= items;    
                    }
                }
            }
            for(int i = 0;i < notifications.size();i++) {
                ACLMessage m = (ACLMessage)notifications.get(i);
                ACLMessage nextRequest = new ACLMessage(ACLMessage.REQUEST);
                nextRequest.addReceiver(m.getSender());
                nextRequest.setProtocol(
                    FIPANames.InteractionProtocol.FIPA_DUTCH_AUCTION);
                //this agent wants to continue negotiation
                if((m.getPerformative() == ACLMessage.INFORM)) {
                    nextRequest.setContent(
                        SellerAgent.buildContent(price, numOfItems));
                } else {
                    nextRequest.setPerformative(ACLMessage.CANCEL);
                }
                nextRequests.add(nextRequest);
            }
            if(numOfItems > 0) {
              ((SellerAgent)myAgent).addMessage("Next price is " + price + 
                ". There are " + numOfItems + " items left for sale.");
            }
        } else {
            ((SellerAgent)myAgent).addMessage("There are no more items for" +
              " sale. The auction is finished.");
            done = true;
        }
    }
}
